// Controller.cs 
// 
//  Copyright © 2009 FreeZzaph
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using System.Collections.Generic;

using LibFreeZzaph;

namespace FreeZzaphGUI
{
	
	internal class Controller : IController
	{
		// Models
		PluginFactory _pluginFactory;
		PluginTreeStore _pluginTreeStore;
		
		// Views
		ISplashView _splash;
		IMainView _main;
		
		#region IController implementation 
		
		public void InitializeApplication ()
		{
			// Show the splash screen
			_splash.ShowSplash();
			
			// Load the plug-ins
			_splash.CertainState = false;
			_splash.Message = "Loading plug-ins...";
			_pluginFactory.LoadPlugins();
			IDictionary<string, IFreeZzaphContentProviderPlugin> cplugins =
				_pluginFactory.ContentProviderPlugins;
			
			// Fetch all the category groups and categories from the plug-ins
			_splash.CertainState = true;
			int pluginCount = cplugins.Count;
			int currentPlugin = 0;
			_splash.PercentDone = 0;
			foreach (IFreeZzaphContentProviderPlugin plugin in cplugins.Values)
			{
				_splash.Message = string.Format("Gathering categories from '{0}'", plugin.Name);
#pragma warning disable 168
				// Need assignment to fetch a property value, don't actually need the value
				var dontCare = plugin.CategoryGroups;
#pragma warning restore 168
				_splash.PercentDone = ((double)currentPlugin++ + 1)/pluginCount;
			}
			
			// Create the plugin treestore
			_pluginTreeStore = new PluginTreeStore(cplugins);
			
			// Hide the splash screen
			_splash.HideSplash();
			_splash = null;
			
			// Show the main window
			_main.PluginTreeStore = _pluginTreeStore;
			_main.ShowMain();
		}
		
		#endregion
		
		public Controller()
		{
			// Create the splash screen
			_splash = new SplashScreen();
			
			// Create the main window
			_main = new MainWindow();
			
			// Create the plugin model
			_pluginFactory = new PluginFactory();
		}
	}
}
